package com.mxpio.erp.technology.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.*;
import com.mxpio.erp.common.technology.service.ProdTecRoutProcTechDrawingSignInService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ProdTecRoutProcTechDrawingSignInServiceImpl extends BaseServiceImpl<ProdTecRoutProcTechDrawingSignIn> implements ProdTecRoutProcTechDrawingSignInService {
    @Override
    @Transactional
    public Result<ProdTecRoutProcTechDrawingSignIn> auditPass(String code, String result) {
        ProdTecRoutProcTechDrawingSignIn signIn = this.getById(ProdTecRoutProcTechDrawingSignIn.class, code);
        if (signIn==null){
            return Result.error("未匹配到数据");
        }
        if (StringUtils.equals(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode(),result)){
            String designFileId = signIn.getDrawingFileId();
            ProdTecRoutProcTechDrawing file = JpaUtil.getOne(ProdTecRoutProcTechDrawing.class, designFileId);
            if (file.getSignOffFlag().equals(CommonEnums.YesNo.YES.getCode())){
                file.setSignOffFlag(CommonEnums.YesNo.NO.getCode());
            }
            file.setPrimary(CommonEnums.YesNo.NO.getCode());
            JpaUtil.update(file);

            ProdTecRoutProcTechDrawing addFile = new ProdTecRoutProcTechDrawing();
            addFile.setFamily(file.getFamily());
            addFile.setVersion(file.getVersion());
            addFile.setEdition(file.getEdition()+1);
            addFile.setRoutProcId(file.getRoutProcId());
            addFile.setFileNo(signIn.getFileNo());
            addFile.setFileName(signIn.getFileName());
            addFile.setLength(signIn.getLength());
            addFile.setExtension(signIn.getExtension());
            addFile.setRemarks(signIn.getRemarks());
            addFile.setSignIner(signIn.getCreateBy());
            addFile.setSignInTime(signIn.getCreateTime());
            addFile.setSignInDesc(signIn.getSignInDesc());
            addFile.setSignInCode(signIn.getCode());//记录新版次数据由那个签入单生成
            JpaUtil.save(addFile);

            signIn.setBpmnStatus(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode());
        }else{
            signIn.setBpmnStatus( OrderStatusEnums.BpmnStatus.CLOSED.getStatusCode());
        }
        update(signIn);
        return Result.OK(signIn);
    }

    @Override
    @Transactional
    public ProdTecRoutProcTechDrawingSignIn getByIdWith(String code) {
        return JpaUtil.linq(ProdTecRoutProcTechDrawingSignIn.class)
                .collect("itemCode", Item.class,"itemCode")
                .collect("id", ProdTecRoutProc.class,"routProcId")
                .equal("code",code).findOne();
    }
}
